package cn.zhuama.org.controller;

import cn.zhuama.basic.util.AjaxResult;
import cn.zhuama.basic.util.PageList;
import cn.zhuama.org.domain.SystemDictionary;
import cn.zhuama.org.query.SystemDictionaryQuery;
import cn.zhuama.org.service.ISystemDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemDictionary")
@Api(value = "系统字典的 API", description = "系统字典相关的 CRUD 功能")
public class SystemdictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过 ID 查询")
    public SystemDictionary findOne(@ApiParam(name = "编号", required = true) @PathVariable("id") Long id) {
        return systemDictionaryService.londOne(id);
    }
    @GetMapping("/type/{type}")
    @ApiOperation(value = "通过 ID 查询")
    public List<SystemDictionary> findType(@ApiParam(name = "编号", required = true) @PathVariable("type") Long type) {
        return systemDictionaryService.londType(type);
    }
    @DeleteMapping("typeDel/{id}")
    public AjaxResult deleteType(@PathVariable("id") Long id) {
        try {

            systemDictionaryService.deleteType(id);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败:" + e.getMessage());
        }
    }

    @GetMapping
    public List<SystemDictionary> findAll() {
        return systemDictionaryService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            systemDictionaryService.removeById(id);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败:" + e.getMessage());
        }
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionary systemDictionary) {
        try {
            if (systemDictionary.getId() == null) {
                systemDictionaryService.save(systemDictionary);
            } else {
                systemDictionaryService.editById(systemDictionary);
            }
            return AjaxResult.me().setMessage("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("编辑失败:" + e.getMessage());
        }
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        try {
            systemDictionaryService.patchDel(ids);
            return AjaxResult.me().setMessage("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败");
        }
    }

    @PostMapping
    public PageList<SystemDictionary> queryPage(@RequestBody SystemDictionaryQuery systemDictionaryQuery) {
        PageList<SystemDictionary> page = systemDictionaryService.queryData(systemDictionaryQuery);
        return page;
    }
}